package com.aop;

import com.web.datasource.DataSourceContextHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import javax.xml.crypto.Data;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/4/26
 * Time: 15:11
 * To change this template use File | Settings | File Templates.
 */
@Aspect
public class DynamicChangeDataSourceAop {

    @Pointcut("execution(* com.dao.impl.*.save*(..)) " +
            "|| execution(* com.dao.impl.*.add*(..)) " +
            "|| execution(* com.dao.impl.*.update*(..)) " +
            "|| execution(* com.dao.impl.*.change*(..)) " +
            "|| execution(* com.dao.impl.*.delete*(..)) " +
            "|| execution(* com.dao.impl.*.del*(..))")
    public void masterPointCut() {
    }

    @Around("masterPointCut()")
    public void matserDataSource(ProceedingJoinPoint joinPoint) {
        try {
            DataSourceContextHolder.master();
            System.out.println("----select master----");
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();    //出错了
        } finally {
            DataSourceContextHolder.remove();
        }
    }


}
